package com.xy.manage.service.impl;


import java.util.List;
import java.util.Map;
import java.util.Date;
import com.xy.core.util.BeanUtil;
import com.xy.common.page.Pagination;
import com.xy.common.util.Configure;
import com.xy.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xy.manage.dao.TmemberDao;
import com.xy.manage.model.Tmember;
import com.xy.manage.service.TmemberService;

@Service
@Transactional
public class TmemberServiceImpl extends BaseServiceImpl<Tmember> implements
		TmemberService {
	private TmemberDao tmemberDao;

	public TmemberServiceImpl(TmemberDao paramAppRoleDao) {
		super(paramAppRoleDao);
		this.tmemberDao = paramAppRoleDao;
	}

	@Override
	public boolean checkExist(String paraName, String paraValue) {
		return tmemberDao.checkExist("Tmember", paraName, paraValue);
	}

	@Override
	public List<Tmember> getTmember(Map<String, Object> map) {
		return tmemberDao.getTmember(map);
	}
	@Override
	public Pagination getTmemberByPage(Map<String, Object> map, int pageNo,
			int pageSize) {
		// TODO Auto-generated method stub
		return tmemberDao.getTmemberByPage(map, pageNo, pageSize);
	}
	 
   @Override
	public Tmember save(Tmember tmember) {
	//	tmember.setModtime(new Date());
		if(tmember.getMemberid()==null){
			//新增
        	return super.save(tmember);
		}else{
			//更新
			Tmember tmember1=tmemberDao.get(tmember.getMemberid());
			try {
				BeanUtil.copyNotNullProperties(tmember1, tmember);
			} catch (Exception e) {
				e.printStackTrace();
			}  
			return super.save(tmember1);
		}
	}

	@Override
	public boolean changeScore(Tmember tmember, int score) {
		try {
			if(null==tmember.getScore()||"".equals(tmember.getScore())){
				tmember.setScore(score+"");
			}else{
				tmember.setScore((Integer.parseInt(tmember.getScore())+score)+"");
			}
			save(changeLevel(tmember));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("+++++++++积分变动错误+++++++++");
			return false;
		}
	}
	
	private Tmember changeLevel(Tmember tmember) {
		try {
			int score = Integer.valueOf(tmember.getScore());
			if (score >= getLevelScore("vip5")) {
				tmember.setUlevel("5");
			} else if (score >= getLevelScore("vip4")) {
				tmember.setUlevel("4");
			} else if (score >= getLevelScore("vip3")) {
				tmember.setUlevel("3");
			} else if (score >= getLevelScore("vip2")) {
				tmember.setUlevel("2");
			} else if (score >= getLevelScore("vip1")) {
				tmember.setUlevel("1");
			} else {
				tmember.setUlevel("0");
			}
		} catch (Exception e) {
			logger.error("+++++++++等级变动错误+++++++++");
		}
		return tmember;
	}
	private int getLevelScore(String propName) throws Exception{
		try {
			return Integer.valueOf(Configure.getProperty(propName));
		} catch (Exception e) {
			logger.error("+++++++++++++"+propName+"is not a number++++++++++++++");
			throw e;
		}
	}
}